﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DB_QLSV.BusinessClassLibrary
{
    public class LopBUS
    {
        private static QLSVDataContext db = new QLSVDataContext();
        public static bool ThemLop(Lop Lop)
        {
            try
            {
                db.Lops.InsertOnSubmit(Lop);
                db.SubmitChanges();
            }
            catch
            {
                //conflict primary key - the item is exist
                return false;
            }
            return true;
        }
        public static bool XoaLop(string idLop)
        {
            try
            {
                Lop old = db.Lops.Single(kh => kh.malop == idLop);
                db.Lops.DeleteOnSubmit(old);
                db.SubmitChanges();
            }
            catch
            {
                // the item is not exist
                return false;
            }
            return true;
        }
        public static bool CapNhatLopHoc(string id, Lop lop)
        {
            try
            {
                Lop old = db.Lops.Single(kh => kh.malop == id);
                old.tenlop = lop.tenlop;
                old.nganh = lop.nganh;
                old.khoahoc = lop.khoahoc;
                db.SubmitChanges();
            }
            catch
            {
                // the item is not exist
                return false;
            }
            return true;
        }
        public static List<Lop> LayDanhSach()
        {
            return db.Lops.ToList();
        }
        public static List<Lop> LayDanhSachTheoMaNganh(String manganh)
        {
            var list = from kq in db.Lops where kq.nganh == manganh select kq;
            List<Lop> result = new List<Lop>();
            foreach (Lop item in list)
            {
                result.Add(item);
            }
            if(result.Count > 0)
                return result;
            return null;
        }
        //get class list depend on branch
        //public static List<Lop> LayDanhSach(Nganh branchObject)
        //{
        //    List<Lop> lst = new List<Lop>();
        //    var query = from rs in db.Lops where branchObject.masonganh == rs.nganh select rs;
        //    foreach (Lop item in query)
        //        lst.Add(item);
        //    if (lst.Count() > 0)
        //        return lst;
        //    return null;
        //}
        //get LopObject depend IdLop
        public static Lop getClassObject(string id)
        {
            var list = from kq in db.Lops where kq.malop == id select kq;
            Lop result = new Lop();
            foreach (Lop item in list)
            {
                result=item;
            }
            if (result!=null)
                return result;
            return null;
        }
        //get subjects depend on BranchId and year
        public static List<StudentManagement.DataGVClassObject> LayDanhSach(Nganh nganhObject,DB_QLSV.KhoaHoc khObject)
        {
            List<StudentManagement.DataGVClassObject> lst = new List<StudentManagement.DataGVClassObject>();
            var query = from lop in db.Lops
                        where lop.nganh == nganhObject.masonganh && lop.khoahoc == khObject.makh
                        select new
                        {
                            lop.malop,
                            lop.khoahoc,
                            lop.tenlop,
                            nganhObject.tennganh
                        };
            foreach (var item in query)
            {
                StudentManagement.DataGVClassObject _new = new StudentManagement.DataGVClassObject();
                _new.malop = item.malop;
                _new.tenlop = item.tenlop;
                _new.tenkhoa = item.khoahoc;
                _new.tennganh = item.tennganh;
                lst.Add(_new);
            }
            if (lst.Count() > 0)
                return lst;
            return null;
        }
        //get subjects depend on BranchId
        public static List<StudentManagement.DataGVClassObject> LayDanhSach(Nganh nganhObject)
        {
            List<StudentManagement.DataGVClassObject> lst = new List<StudentManagement.DataGVClassObject>();
            var query =  from lop in db.Lops
                         where lop.nganh == nganhObject.masonganh
                        select new
                        {
                            lop.malop,
                            lop.khoahoc,
                            lop.tenlop,
                            nganhObject.tennganh
                        };
            foreach (var item in query)
            {
                StudentManagement.DataGVClassObject _new = new StudentManagement.DataGVClassObject();
                _new.malop = item.malop;
                _new.tenlop = item.tenlop;
                _new.tenkhoa = item.khoahoc;
                _new.tennganh = item.tennganh;
                lst.Add(_new);
            }
            if (lst.Count() > 0)
                return lst;
            return null;
        }
    }
}
